// 引入 echarts 文件
import * as echarts from '../../../../ec-canvas/echarts';
import {exportDataText,exportDataFile} from '../../../../api/api';
import exportFunc from '../../../../utils/fileUnit';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    allData:[],
    wordLabelList:[],
    classificationList:[],
    textLabelList:[],
    // 此处的ec名称与wxml结构中命名保持一致
    ec: {
      // 使用 onInit 方法定义
      // onInit: initChart
      lazyLoad: true
    },
    distributionList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(JSON.parse(options.data));
    this.setData({
      allData: JSON.parse(options.data),
    })

    //获取组件
    this.lazyComponent = this.selectComponent('#myCharts')
    const textLabel = this.data.allData.textLabel;
    const wordLabel = this.data.allData.wordLabel.split(",");
    const classification = this.data.allData.classification;
    this.setData({
      wordLabelList: wordLabel,
      classificationList: classification,
      textLabelList: textLabel
    })
    //模拟请求
    setTimeout(()=> {
      const distribution = this.data.allData.distribution.split(',');
      this.setData({
        distributionList: distribution
      })
      //初始传入数据
      this.init(this.data.distributionList)
    },500)
  },
  //下载文本
  downloadText(){
    exportDataText({
      id: encodeURI(this.data.allData.id)
    }).then(res=>{
      exportFunc(res);
    }).catch(error=>{
      console.log(error);
    })
  },
    //下载报告
  downloadFile(){
    exportDataFile({
      id: this.data.allData.id
    }).then(res=>{
      console.log(res);
      exportFunc(res);
    }).catch(error=>{
      console.log(error);
    })
  },

  //手动初始化
  init(optionData){
    this.lazyComponent.init((canvas,width,height,dpr)=>{
      let chart = echarts.init(canvas,null,{
        width: width,
        height: height,
        devicePixelRatio: dpr // 像素
      })

      let option = getOption(optionData)

      chart.setOption(option)

      //图表示例绑定this上方便在其他函数访问
      this.chart = chart
      return chart
    })
  
  // 定义 getOption 方法
  function getOption(data) {
    // 此为配置项。配置图表展现样式与数据
    return{
      xAxis: {
        type: 'category',
        // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: data,
          type: 'line'
        }
      ]
    }
  }}
})